package leetcode.code003.LongestSubstringWithoutRepeatingCharacters;

/**
 * 一次遍历
 *
 * @author Max
 * @date 2021年8月17日
 * @description
 */
public class Solution3 extends LongestSubstringWithoutRepeatingCharacters {

	@Override
	public int lengthOfLongestSubstring(String s) {
		// 记录字符上一次出现的位置
		int[] last = new int[128];
		for (int i = 0; i < 128; i++) {
			last[i] = -1;
		}
		int n = s.length();
		char[] array = s.toCharArray();
		int res = 0;
		int start = 0; // 窗口开始位置
		for (int i = 0; i < n; i++) {
			int index = array[i];
			start = Math.max(start, last[index] + 1);
			res = Math.max(res, i - start + 1);
			last[index] = i;
		}
		return res;
	}

	public static void main(String[] args) {
		Solution3 so = new Solution3();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}
}
